package com.moleq.mgpos.activities;

import java.io.File;

import com.moleq.itemsetting.adapter.FilePickerAdapter;
import com.moleq.itemsetting.models.FileData;
import com.moleq.mgpos.activities.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Toast;

public class ImportDataFilePickerActivity extends Activity {

	private ListView listViewFilePicker;
	private File mCurrentDirectory; //��Ŀ¼λ��
	FilePickerAdapter mFileAdapter; //ListView������
	private ImageView iv_cancel;
	String fileEndings[] = { "csv" }; 	//��ʾ���ļ�����
	String filePathString = "";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.filelist);
		listViewFilePicker = (ListView) findViewById(R.id.filelist);
		listViewFilePicker.setCacheColorHint(0x00000000);
		mFileAdapter = new FilePickerAdapter(this);
		listViewFilePicker.setAdapter(mFileAdapter);
		iv_cancel = (ImageView) findViewById(R.id.iv_cancel);
		iv_cancel.setOnClickListener(onClickListener);
		
		
		Intent intent = getIntent();
		filePathString = intent.getStringExtra("filePath");
		if(!filePathString.equals("")){
			mCurrentDirectory = new File(filePathString);
		}else{
			mCurrentDirectory = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
		}
		//Item����¼�
		ListView.OnItemClickListener lv2click = new ListView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView parent, View view,
					int position, long id) {
				int fid = mFileAdapter.getItemType((int) id);
				String mPath = "";
				try{
					if (fid == 1) { //�ļ�����1Ϊ�ļ��У����������ļ���Ŀ¼
						String s1 = mFileAdapter.getItem((int) id).name;
						if (s1.equals("Back...")) {
							mPath = mCurrentDirectory.getParent();
						} else {
							mPath = mCurrentDirectory.getPath() + "/" + s1 + "/";
						}
						mCurrentDirectory = new File(mPath);
						ListFile(mCurrentDirectory);
					} else { //����ѡ�е��ļ���Path
						Bundle bundle = new Bundle();
						bundle.putString("path", mCurrentDirectory.getPath()
								+ "/" + mFileAdapter.getItem((int) id).name);
						bundle.putString("filePath", mCurrentDirectory.getPath());
						Intent mIntent = new Intent();
						mIntent.putExtras(bundle);
						setResult(RESULT_OK, mIntent);
						ImportDataFilePickerActivity.this.finish();
					}
				} catch (Exception e) {
					Toast.makeText(ImportDataFilePickerActivity.this, "Can not operate.", Toast.LENGTH_LONG)
					.show();
					String error = e.getMessage();
					return;
				}
			}
		};
		ListFile(mCurrentDirectory);
		listViewFilePicker.setOnItemClickListener(lv2click);
	}
	
	private OnClickListener onClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.iv_cancel:
				Intent mIntent = new Intent();
				setResult(RESULT_OK, mIntent);
				ImportDataFilePickerActivity.this.finish();
				break;
			}
		}
		
	};

	/**
	 * �г���Ŀ¼���ļ�
	 * @param aDirectory
	 */
	private void ListFile(File aDirectory) {
		mFileAdapter.clearItems();
		mFileAdapter.notifyDataSetChanged();
		listViewFilePicker.postInvalidate();
		if (!aDirectory.getPath().equals("/sdcard")) {
			FileData fd = new FileData();
			fd.name = "Back...";
			fd.type = 1;
			mFileAdapter.addItem(fd);
		}
		for (File f : aDirectory.listFiles()) {
			if (f.isDirectory()) {
				FileData fd = new FileData();
				fd.name = f.getName();
				fd.type = 1;
				mFileAdapter.addItem(fd);
			} else {
				if (checkEnds(f.getName().toLowerCase())) {
					FileData fd = new FileData();
					fd.name = f.getName();
					fd.type = 0;
					mFileAdapter.addItem(fd);
				}
			}
		}
		mFileAdapter.notifyDataSetChanged();
		listViewFilePicker.postInvalidate();
	}

	private boolean checkEnds(String checkItsEnd) {
		for (String aEnd : fileEndings) {
			if (checkItsEnd.endsWith(aEnd))
				return true;
		}
		return false;
	}
}
